【发布时间】:2014-08-07 14:47:20
【问题描述】:
我正在尝试将一个 android 应用程序移植到 windows phone 应用程序中,并且我坚持执行 json 解析任务。谁能指导我如何在 c# 中实现。
JSON
{
"chapter1": {
"name": "Successful Sales",
"page1": {
"url": "Chapter-304/Page-1.html"
},
"page2": {
"url": "Chapter-304/Page-2.html"
},
"page3": {
"url": "Chapter-304/Page-3.html"
},
"page4": {
"url": "Chapter-304/Page-4.html"
},
"page5": {
"url": "Chapter-304/Page-5.html"
},
"page6": {
"url": "Chapter-304/Page-6.html"
},
"page7": {
"url": "Chapter-304/Page-7.html"
},
"page8": {
"url": "Chapter-304/Page-8.html"
},
"page9": {
"url": "Chapter-304/Page-9.html"
},
"page10": {
"url": "Chapter-304/Page-10.html"
}
}
}
安卓代码
JSONObject jsonObject = (JSONObject) new JSONTokener(courselist).nextValue();
/*courselist - nothing but json string*/
int jsonLength = jsonObject.length();
JSONObject[] subJson = new JSONObject[jsonLength];
String[] courseString = new String[jsonLength];
Vector<Object> mVectorElements = new Vector<Object>();
for (int i = 0; i < jsonLength; i++) {
try {
subJson[i] = jsonObject.getJSONObject(jsonObject.names()
.getString(i));
courseString[i] = jsonObject.names().getString(i) + "@@@@"
+ subJson[i].getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
Arrays.sort(courseString);
for (int j = 0; j < courseString.length; j++) {
mVectorElements.add(courseString[j].split("@@@@")[1]);
}
我正在尝试使用 Newtonsoft.Json.Linq.JToken 解析,但解析后我将如何获取每个元素。如果我使用错误的方式,请纠正我。
更新: 下面是我试图在 JObject 中获取属性名称但循环不起作用的方法。
jsonObject = (JObject) JToken.Parse(courselist);
foreach (JObject content in jsonObject.Children<JObject>())
{
foreach (JProperty prop in content.Properties())
{
Debug.WriteLine(prop.Name);
}
}
【问题讨论】:
-
您正在向我们展示 Android 代码,但您正在询问如何在 C# 中执行此操作。请同时显示 Android 代码和 C# 代码,以便我们查看您的位置。
-
@SlashTag 确定请检查更新。
标签: c# android json windows-phone-8 windows-phone