如何将JSON字符串值赋予Android String Variable?

时间:2014-02-08 19:01:08

标签: android json

我有一个JSON字符串,我想将JSON字符串分配到Android字符串变量中,以将JSON对象提取为单独的字符串。

String str="{"abc":[{"Fesa1":"english is good "},{"Fesb1":"my english is not so accurate"},{"Fesa2":"german is very nice language"},{"Fesb2":"my german is absolutely good"},{"Fesa3":"cricket is a very popular game "},{"Fesb3":"cricket is life for indian"},{"Fesa4":"hockey is the original game for india"},{"Fesb4":"hockey needs to improve "},{"Fesa5":"computer is a very functional device"},{"Fesb5":"computer is very helpful to mankind"}]}"; 

在将JSON字符串值赋予 str 变量时,它会给出错误。我们不能将单引号引入JSON字符串,以便如何解决这种冲突。

请提出一些解决方案。

提前致谢!!!

4 个答案:

答案 0 :(得分:2)

就像这样:

String json = "{\"username\":\""+username+"\",\"password\":\""+password+"\"}";

测试并使用我的Android应用程序

答案 1 :(得分:1)

使用link here转义JSON字符串。

答案 2 :(得分:1)

您需要在JSON字符串中转义特殊字符(即\“)。

这是正确的版本:

String str = "{\"abc\":[{\"Fesa1\":\"english is good \"},{\"Fesb1\":\"my english is not so accurate\"},{\"Fesa2\":\"german is very nice language\"},{\"Fesb2\":\"my german is absolutely good\"},{\"Fesa3\":\"cricket is a very popular game \"},{\"Fesb3\":\"cricket is life for indian\"},{\"Fesa4\":\"hockey is the original game for india\"},{\"Fesb4\":\"hockey needs to improve \"},{\"Fesa5\":\"computer is a very functional device\"},{\"Fesb5\":\"computer is very helpful to mankind\"}]}";

干杯!

答案 3 :(得分:-1)

使用以下

String jsonstring = "{"
        +"abc"+": ["
            +"{"
               + "Fesa1"+":"+ "englishisgood"
            +"},"
            +"{"
                +"Fesb1"+":"+ "myenglishisnotsoaccurate"
            +"},"
             +"{"
               + "Fesa2"+":"+ "germanisverynicelanguage"
            +"}"+","
            +"{"
                +"Fesb2"+":"+ "mygermanisabsolutelygood"
            +"},"
            +"{"
                +"Fesa3"+":"+ "cricketisaverypopulargame"
            +"},"
            +"{"
                +"Fesb3"+":"+ "cricketislifeforindian"
            +"},"
            +"{"
                +"Fesa4"+":"+  "hockeyistheoriginalgameforindia"
            +"},"
            +"{"
                +"Fesb4"+":"+  "hockeyneedstoimprove"
            +"},"
            +"{"
                +"Fesa5"+":"+  "computerisaveryfunctionaldevice"
           +"},"
           +"{"
                +"Fesb5"+":"+  "computerisveryhelpfultomankind"
            +"},"
        +"]"+
    "}";