本文介绍了如何获取UTF-8 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LitJSON库,但情况有些奇怪.

I'm using LitJSON library but things gets a little bit odd.

您知道转换时保留重音符号的任何JSON库吗?

Do you know any JSON library that keeps the accents when converting ?

这是测试:

test.json

test.json

[{"id":"CS_001","name":"L'élément","type":"Tôt"},{"id":"CS_002","name":"L'outrage","type":"Tôt"},{"id":"CS_003","name":"Test","type":"Tôt"}]

test.cs

public class test : MonoBehaviour {
    private string jsonString;
    private JsonData cardData;
    JsonData database;

    void Start () {
        jsonString = File.ReadAllText (Application.dataPath + "/test.json");
        cardData = JsonMapper.ToObject (jsonString);
        database = JsonMapper.ToJson (cardData);
        Debug.Log (database.ToString ());
    }
}

然后Debug.Log变为:

And the Debug.Log turns to :

[{"id":"CS_001","name":"L'\u00E9l\u00E9ment","type":"T\u00F4t"},{"id":"CS_002","name":"L'outrage","type":"T\u00F4t"},{"id":"CS_003","name":"Test","type":"T\u00F4t"}]

有人知道如何获得合适的Json吗?即使与另一个JSON库一起使用.

Any idea how to get a proper Json ? Even if it's with another JSON library.

非常感谢您.

推荐答案

以下是使用 Json.Net 反序列化字符串:

Here's an example using Json.Net to deserialize the string:

using System;
using Newtonsoft.Json;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        // Deserialize the JSON into a list of CardData
        var ob = JsonConvert.DeserializeObject<List<CardData>>("[{\"id\":\"CS_001\",\"name\":\"L'élément\",\"type\":\"Tôt\"},{\"id\":\"CS_002\",\"name\":\"L'outrage\",\"type\":\"Tôt\"},{\"id\":\"CS_003\",\"name\":\"Test\",\"type\":\"Tôt\"}]" );

        /*
          The output will be:
            id: CS_001, name: L'élément, type: Tôt
            id: CS_002, name: L'outrage, type: Tôt
            id: CS_003, name: Test, type: Tôt
        */
        foreach(var i in ob){
            Console.WriteLine(i);
        }
    }
}

// Class that will hold the deserialized data
// For demo puposes
public class CardData
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }

    public override string ToString(){
        return String.Format("id: {0}, name: {1}, type: {2}",id, name, type);
    }
}

在线演示可在此处

这篇关于如何获取UTF-8 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:38