กำลังเขียนไฟล์ .json และอ่านไฟล์นั้นใน Android

ฉันกำลังเขียนไฟล์ .json และฉันต้องการอ่านไฟล์นั้น แต่ปัญหาคือ เมื่อฉันพยายามอ่านไฟล์ทั้งหมดในรูปแบบสตริง มันจะเพิ่มช่องว่างก่อนและหลังอักขระทุกตัว และเนื่องจากอักขระพิเศษ ทำให้ไม่สามารถอ่าน json ได้

รูปแบบ Json คือ

[{"description1":"The ThinkerA bronze sculpture by Auguste Rodin. It depicts a man in sober\nmeditation battling with a powerful internal struggle.","description2":"Steve JobsFounder of Apple, he is widely recognized as a charismatic pioneer of\nthe personal computer revolution.","description3":"Justin BieberBorn in 1994, the latest sensation in music industry with numerous\nawards in recent years."}]

แต่มันให้คำตอบแปลกๆ เช่น [ { " d e s c r i p t i o n 1 " : " T h e .....

เพื่อตัดช่องว่างเพิ่มเติมที่ฉันอ้างถึงสิ่งนี้ แต่ก็ยังใช้งานไม่ได้: Java วิธีแทนที่ 2 ช่องว่างขึ้นไปด้วยช่องว่างเดียวในสตริงและลบช่องว่างนำหน้าเท่านั้น

ฉันใช้รหัสนี้

File folderPath = Environment.getExternalStorageDirectory();
File mypath=new File(folderPath, "description.json");
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(mypath));
char[] buf = new char[1024];
int numRead=0;

while((numRead=reader.read(buf)) != -1)
{
    String readData = String.valueOf(buf, 0, numRead);
    fileData.append(readData);
    buf = new char[1024];
}
String response = fileData.toString();

สตริง "response" มีการตอบสนองที่แปลก

แล้วมีใครช่วยฉันได้บ้าง?

สำหรับการเขียนลงในไฟล์ฉันใช้:

FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);

person SML    schedule 08.01.2013    source แหล่งที่มา
comment
BufferedReader มีวิธีการ readLine ลองใช้ดูนะครับ   -  person Mikita Belahlazau    schedule 08.01.2013
comment
คุณสามารถโพสต์รหัสที่คุณใช้ในการเขียนไฟล์ได้หรือไม่? ดูเหมือนว่าคุณอาจจะเขียนอักขระในรูปแบบ 16 บิต   -  person vaughandroid    schedule 08.01.2013
comment
ลองเปิดไฟล์และดูว่าเนื้อหามีช่องว่างหรืออักขระที่มองไม่เห็นหรือไม่ ลองบันทึกเพื่อดูว่ามีอะไรส่งคืนใน numRead และ readData สำหรับการวนซ้ำแต่ละครั้ง นี่คือการลดขอบเขตและตรวจสอบให้แน่ใจว่าปัญหามาจากการอ่านไม่เขียน   -  person RobGThai    schedule 08.01.2013
comment
@RobGThai ฉันกำลังเขียนโดยใช้ writeChars ซึ่งนำไปสู่การเขียนช่องว่างก่อนตัวอักษรทุกตัว ตอนนี้ฉันใช้ writeUTF และแก้ไขปัญหาแล้ว ขอขอบคุณทุกท่านสำหรับการแบ่งปันความคิดเห็นของพวกเขา   -  person SML    schedule 09.01.2013


คำตอบ (4)


writeChars เขียนอักขระแต่ละตัวเป็นสองไบต์

http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChars(java.lang.String)

http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChar(int)

Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.
person auselen    schedule 09.01.2013

เขียนวิธีการเขียนไฟล์ Json ด้านล่าง โดยที่ params คือชื่อไฟล์ และ mJsonResponse คือการตอบสนองของเซิร์ฟเวอร์

สำหรับสร้างไฟล์ลงในหน่วยความจำภายในของแอปพลิเคชัน

public void mCreateAndSaveFile(String params, String mJsonResponse) {
    try {
        FileWriter file = new FileWriter("/data/data/" + getApplicationContext().getPackageName() + "/" + params);
        file.write(mJsonResponse);
        file.flush();
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

สำหรับการอ่านข้อมูลจากไฟล์ Json ที่นี่ params คือชื่อไฟล์

public void mReadJsonData(String params) {
    try {
        File f = new File("/data/data/" + getPackageName() + "/" + params);
        FileInputStream is = new FileInputStream(f);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        String mResponse = new String(buffer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
person Dipak Keshariya    schedule 09.01.2013

ฉันชอบคำตอบข้างต้นและแก้ไข: ฉันแค่ชอบแบ่งปันดังนั้นฉันจึงแชร์ที่อาจเป็นประโยชน์กับผู้อื่น

คัดลอกและวางคลาสต่อไปนี้ในแพ็คเกจของคุณและใช้เช่น:

บันทึก: MyJSON.saveData(context, jsonData);

อ่าน: String json = MyJSON.getData(context);

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Pratik.
 */
public class MyJSON {

    static String fileName = "myBlog.json";

    public static void saveData(Context context, String mJsonResponse) {
        try {
            FileWriter file = new FileWriter(context.getFilesDir().getPath() + "/" + fileName);
            file.write(mJsonResponse);
            file.flush();
            file.close();
        } catch (IOException e) {
            Log.e("TAG", "Error in Writing: " + e.getLocalizedMessage());
        }
    }

    public static String getData(Context context) {
        try {
            File f = new File(context.getFilesDir().getPath() + "/" + fileName);
            //check whether file exists
            FileInputStream is = new FileInputStream(f);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            return new String(buffer);
        } catch (IOException e) {
            Log.e("TAG", "Error in Reading: " + e.getLocalizedMessage());
            return null;
        }
    }
}
person Pratik Butani    schedule 06.11.2015

รหัสการเขียนของคุณเป็นปัญหา เพียงแค่ใช้

FileWriter fos = new FileWriter(mypath);
fos.write(response);
person Henry    schedule 09.01.2013